var leads = new Array();
var leadMarkers = new Array();			
var rowCounter1 = 1;	
var rowCounter2 = 1;	
var headLine;
var sLine;
var date;
var col1;
var col2;
var col3;
var localRotSearch;
var howTo;
var UpdatesObj;

function createSearchTab(name,whichSearch) 
{
       
        switch (whichSearch) 
        {
            case frndSearch:
            {
                headLine = "Plans made by";
                sLine = name.getDisplayName();
                col1 = "Place";
                col2 = "Date";
                initTable();
                loadFriendPlans(name);
                break;
            }
            case dtSearch: 
            {
                headLine = "Plans made on the";
                sLine = name;
                date = name;
                col1 = "Place";
                col2 = "Person";
                initTable();
                searchByDate();
                break;
            }
            case pSearch: 
            {
                headLine = "Plans made in";
                sLine = name.titleNoFormatting;
                col1 = "Date";
                col2 = "Person";
                initTable();
                searchByPlace(name);
                break;
            }
        }      
            
 }
 
 function initTable()        
 {
        document.getElementById("theDiv").innerHTML = "";
        document.getElementById("selectfriends").style.display = 'inline';
		document.getElementById("selectfriends").innerHTML =  '<id="header" class="sb-result">'+
													    	  '<td class="sb-row"></td>'+
														      '<td class="sb-header"> <b>'+headLine+' </b>' +
														      '<b>'+sLine+':</b></td>';
		document.getElementById("selectfriends").innerHTML += '<div id="Searchtable1"></div>';
		       
		url = "http://www.cs.tau.ac.il/~hilacoh1/images/ajax-load.gif";
		rowCounter1 = 1;									
		document.getElementById("Searchtable1").innerHTML =   		'<table width="100%"><tr><td valign="top">'
																	+'<table id="dataheader" class="sb-result">'
																	+	'<td class="sb-row"></td>'
																	+	'<td class="sb-col1">'+col1+' </td>'
																	+	'<td class="sb-col2">'+col2+' </td>'
																	+ '</table>'
																	+  '<div id="Friendresults" class="visible"><img src='+url+' id="loadF"/></div>'
																	+ '</td></tr></table>';
																	
																	
		
}


function loadFriendPlans(person)
{
    howTo = 0;    
     var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
	var url = GLOBAL_BASE_URL + '/updates';
					
	postData = gadgets.io.encodeValues({
		'sender_id' : person.getId()
		});
					
          
    params[gadgets.io.RequestParameters.POST_DATA] = postData;
    gadgets.io.makeRequest(url, addToSearchTable, params);
				
				
				
}

function siteSearch(i)
{       
		var site = UpdatesObj[i].site;
        var lat = UpdatesObj[i].lat;
        var lng = UpdatesObj[i].lng;
 	    localRotSearch.setCenterPoint(new GLatLng(lat,lng));
		localRotSearch.setSearchCompleteCallback(this, openSite);
		localRotSearch.execute(site);
}

function openSite()
{
	window.open(localRotSearch.results[0].url);
}

function addToSearchTable(response) 
{
    localRotSearch = new google.search.LocalSearch();
    
    friends = response.data;
	UpdatesObj = gadgets.json.parse(friends);

	
	// remove loading animation
	if (rowCounter1 == 1) 
	document.getElementById("loadF").style.display="none";
	
	
	
	for (var i = 0; i < UpdatesObj.length; i++)
    {	
    
	var t = createTable("sb-result");
	var tr = createTableRow(t);
	var rowCell = createTableCell(tr,"sb-row");
	var firstCell = createTableCell(tr,"sb-col1");
	var secondCell = createTableCell(tr,"sb-col1");
	
	
	    var onclick = function(i) {return 'siteSearch(' + i +  ')';};

	    var str = rowCounter1++;
	    rowCell.innerHTML = str;
     

        str = '<a href="#" onclick= ' + onclick(i) +">" + UpdatesObj[i].site + "</a>";
    	firstCell.innerHTML = str;


    
    if (howTo == 0)
    {
	    str = UpdatesObj[i].date;
	    secondCell.innerHTML = str;
	    
	}
	
	if (howTo == 1)
	{
	    str = isfriend(UpdatesObj[i].id);
	    var url = "http://www.google.com/s2/profiles/"+UpdatesObj[i].id;
	    secondCell.innerHTML = "<a href="+url+" target='_blank'>" + str + "</a>";
	}
    
	    // attach it
	    var savedResults = document.getElementById("Friendresults");
	    savedResults.appendChild(t);
    }
	if (UpdatesObj.length == 0) {
		displayNoResults(3);
	}
}


function searchByDate() 
{
    howTo = 1;    
	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
	var url = GLOBAL_BASE_URL + '/search';
	
	postData = gadgets.io.encodeValues({
		'method'  : '0',
		'Date' : date
	});

	params[gadgets.io.RequestParameters.POST_DATA] = postData;
	gadgets.io.makeRequest(url, addToSearchTable, params);
}

function isfriend(idText)
{
    for (var i=0; i<myFriendsNames.length;i++)
    {
        if (idText == myFriendsNames[i].getId())
        {
            return myFriendsNames[i].getDisplayName();
        }
    }
    return "A user";
}

function searchByPlace(result)
{
    howTo = 1;
    var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
	var url = GLOBAL_BASE_URL + '/search';

	postData = gadgets.io.encodeValues({
		'method'  : '1',
		'site' : result.titleNoFormatting,
		'lat' : result.lat,
		'lng' : result.lng
		
	});

	params[gadgets.io.RequestParameters.POST_DATA] = postData;
	gadgets.io.makeRequest(url, addToSearchTable, params);
}
